package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.Fc;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class FcForm extends BasePage implements Serializable {
    private GenericManager<Fc, Long> fcManager;
    private Fc fc = new Fc();
    private Long id;
    
    
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setFcManager(GenericManager<Fc, Long> fcManager) {
        this.fcManager = fcManager;
    }

    public Fc getFc() {
        return fc;
    }

    public void setFc(Fc fc) {
        this.fc = fc;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        fcManager.remove(fc.getId());
        addMessage("fc.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            fc = fcManager.get(id);
        } else {
            fc = new Fc();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (fc.getId() == null || fc.getId() == 0);
        // set the related jbqk as a foreign key.
        fc.setJbqk( jbqk );
        fcManager.save(fc);

        String key = (isNew) ? "fc.added" : "fc.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 